﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Core.AdminGroup;
using Core.AdminGroupMenuPermission;
using Core.AdminMenu;
using Core.Authenticated;
using Core.Library;

namespace CMS.Controls.AdminGroup
{
    public partial class AdminGroupMenu : AuthenticatedControl
    {
        private List<AdminGroupMenuPermissionObject> _lstPermission;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var groupId = Utils.GetIdFromRequestString(Utils.GroupId, 0);

                if (groupId > 0)
                {
                    ListAdminGroupPermission(groupId);
                    ltMessage.Text = MessageUtility.ShowMessage(string.Empty,
                                                                string.Format("Phân quyền chức năng cho nhóm '{0}'", GetGroupName(groupId)),
                                                                MessageType.Information);
                }
            }
        }

        private string GetGroupName(int groupId)
        {
            try
            {
                var group = AdminGroupController.AdminGroup_GetById(groupId);
                return group != null ? group.Name : string.Empty;
            }
            catch { return string.Empty; }
        }

        private void ListAdminGroupPermission(int groupId)
        {
            _lstPermission = AdminGroupMenuPermissionController.AdminGroupMenuPermission_GetByAdminGroupId(groupId);

            var listMenu = new List<AdminMenuObject>();
            AdminMenuController.AdminMenu_GetTree(listMenu, 0, 0);

            if (listMenu.Count > 0)
            {
                lvGroupMenu.DataSource = listMenu;
                lvGroupMenu.DataBind();
            }
        }

        public static void FillPermissionCheckList(CheckBoxList o, long menuId, int currentStatus)
        {
            o.Items.Clear();

            o.Items.Add(new ListItem("Xuất bản", PermissionObject.CanPublish.ToString()));
            o.Items[0].Selected = StatusHelper.IsChecked(currentStatus, (int)PermissionObject.CanPublish);

            o.Items.Add(new ListItem("Trả bài", PermissionObject.CanUnlock.ToString()));
            o.Items[1].Selected = StatusHelper.IsChecked(currentStatus, (int)PermissionObject.CanReturn);
            
            //o.Items.Add(new ListItem("Gỡ khóa", PermissionObject.CanUnlock.ToString()));
            //o.Items[4].Selected = StatusHelper.IsChecked(currentStatus, (int)PermissionObject.CanUnlock);
        }

        protected void BtnSave(object sender, EventArgs e)
        {
            var groupId = Utils.GetIdFromRequestString(Utils.GroupId, 0);
            if (groupId == 0)
                return;

            // Delete Admin Group Menu Permission By AdminGroupId
            AdminGroupMenuPermissionController.AdminGroupMenuPermission_Delete(groupId);

            foreach (var lvi in lvGroupMenu.Items)
            {
                var cb = (CheckBox)lvi.FindControl("cbItem");

                if (cb == null || !cb.Checked) continue;

                var menuId = Convert.ToInt32(lvGroupMenu.DataKeys[lvi.DisplayIndex].Value);

                // Save Admin Group Industry Permission
                var agmp = new AdminGroupMenuPermissionObject()
                {
                    AdminGroupId = groupId,
                    AdminMenuId = menuId,
                    Status = 0
                };

                var chkLstPermission = (CheckBoxList)lvi.FindControl("chkLstPermission");
                if (chkLstPermission.Items[0].Selected)
                    agmp.Status += (int)PermissionObject.CanPublish;
                if (chkLstPermission.Items[1].Selected)
                    agmp.Status += (int)PermissionObject.CanReturn;                                
                //if (chkLstPermission.Items[4].Selected)
                //    agmp.Status += (int)PermissionObject.CanUnlock;
                
                AdminGroupMenuPermissionController.AdminGroupMenuPermission_Create(agmp);
            }

            ListAdminGroupPermission(groupId);

            ltMessage.Text = MessageUtility.ShowMessage(string.Empty,
                                                        string.Format("Phân quyền chức năng cho nhóm '{0}' thành công!", GetGroupName(groupId)),
                                                        MessageType.Success);

        }

        protected void lvGroupMenu_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                var menu = (AdminMenuObject)e.Item.DataItem;
                var cbItem = (CheckBox)e.Item.FindControl("cbItem");
                var chkLstPermission = (CheckBoxList)e.Item.FindControl("chkLstPermission");
                var rowStatus = 0;

                foreach (var o in _lstPermission)
                    if (o.AdminMenuId == menu.AdminMenuId)
                    {
                        cbItem.Checked = true;
                        rowStatus = o.Status;
                    }
                FillPermissionCheckList(chkLstPermission, menu.AdminMenuId, rowStatus);
            }
        }
    }
}